const Service = require('egg').Service;
const { Sequelize, Op } = require('sequelize');

class StudentService extends Service {
    // 添加用service
    async add(student) {
        const { ctx } = this;
        const result = await ctx.model.Student.create(student);
        return result;
    }
    async findList(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Student.findAndCountAll({
            where: {
                name: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    // 根据id查找对应的值
    async findById(id) {
        const { ctx } = this;
        const result = await ctx.model.Student.findByPk(id);
        return result;
    }
    // 修改
    async modify(student) {
        const { ctx } = this;
        const { 
            id,
            name,
            gender,
            birth,
            subject,
            telephone,
            picture,
            progress,
            reviewed,
        } = student;
        console.log(' await ctx.model.Student.update');
        const result = await ctx.model.Student.update({
            name,
            gender,
            birth,
            subject,
            telephone,
            picture,
            progress,
            reviewed,
        }, {
            where: { id }
        });
        return result;
    }
    // 删除
    async remove(id) {
        const { ctx } = this;
        const result = await ctx.model.Student.update({
            deleted:1
        },{
            where: { id }
        });
        return result;
    }
}

module.exports = StudentService;
